package com.bupt.liutong.front.groupon.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.groupon.bo.CityBo;
import com.bupt.liutong.admin.groupon.form.UserForm;
import com.bupt.liutong.util.StringUtils;

public class CityAction extends BaseAction {
	
	@Autowired
	private CityBo cityBo;

	public ActionForward selCity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form;
		// remove
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				System.out.println(cookie.getName()+"="+cookie.getValue());
				if (cookie.getName().equalsIgnoreCase("baiducity")) {
					cookie.setMaxAge(0);
				}
			}
		}
		// add
		Cookie c = new Cookie("baiducity", userForm.getCityId().toString());
		c.setMaxAge(60*60*24*365);
		response.addCookie(c);
		if (!StringUtils.isEmpty(userForm.getUid())) {
			cityBo.updateUser(userForm);
		}
		String path = mapping.findForward("groupon_list").getPath();
		path += ("&cityId=" + userForm.getCityId());
		return new ActionForward(path, true);
	}

	public void setCityBo(CityBo cityBo) {
		this.cityBo = cityBo;
	}

}
